package com.topnav.mc.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.js.lib.cache.Cache;
import com.js.lib.cache.listener.AbstractMessageListener;
import com.js.lib.cache.listener.EventType;
import com.js.lib.cache.listener.MessageListener;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.DeviceChannel;
import com.topnav.mc.admin.entity.DeviceLog;
import com.topnav.mc.admin.service.IDeviceChannelService;
import com.topnav.mc.admin.service.IDeviceLogService;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.position.PositionInfo;
import com.topnav.mc.position.PositionUtil;
import com.topnav.mc.uitls.DeviceOnlineUtils;
import com.topnav.mc.uitls.MapUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

import static com.topnav.mc.common.Constant.DEVICE_CURRENT_STATUS;

/**
 * 监听本地缓存的KEY 的事件
 * @Author: kenny
 * @Date: 2023/11/1 17:12
 */
@Slf4j
@Component
@Data
public class DeviceExpireLocalCachListener extends AbstractMessageListener {
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IDeviceChannelService deviceChannelService;
    @Autowired
    private PositionUtil positionUtil;
    @Autowired
    private IDeviceLogService deviceLogService;
    @Autowired
    private DeviceOnlineUtils deviceOnlineUtils;
/*    @Resource(name="sysCache")
    private Cache sysCache;*/

    @Override
    public void onMessage(String event,Object key, Object item,@Nullable byte[] value){
        //log.info(">>>>>>>>{} Key: {},{}",event,key,item);
        //设备离线
        if (item != null){
            log.info(">>>>>>>>心跳过期，设备下线 {}",item.toString());
            deviceOnlineUtils.setOffline(item.toString());
            deviceOnlineUtils.deviceStatusEvent(deviceService.getDeviceFromCache(item.toString()),0,"心跳过期");

/*            Device device = deviceService.getDeviceFromCache(item.toString());
            if (device != null){
                if (device.getOnline()){
                    deviceOnlineUtils.setOffline(device.getCode());
                }
                deviceOnlineUtils.deviceStatusEvent(device,0);
            }*/
        }
    }

    @Override
    public String getResolveKey(){
        return DEVICE_CURRENT_STATUS;
    }
    @Override
    public String getResolveItem(){
        return "*";
    }
    @Override
    public EventType getEventType(){
        return EventType.EXPIRE;
    }
}
